<?php
// +----------------------------------------------------------------------
// | Quotes [ 只为给用户更好的体验]**[我知道发出来有人会盗用,但请您留版权]
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 青涩  <3489621943@qq.com>          盗用不留版权,你就不配拿去!
// +----------------------------------------------------------------------
// | Date: 2021年03月24日
// +----------------------------------------------------------------------
include("../includes/common.php");
try{
    function success($data,$msg='获取成功'){
    echo json_encode(array_merge([
        'code'=>1,
        'msg'=>$msg
    ],$data));
    exit();
}
function error($msg){
    echo json_encode([
        'code'=>-1,
        'msg'=>$msg
    ]);
    exit();
}
$action = $_REQUEST['act'];
    file_put_contents(__DIR__.'/request.txt',var_export($_REQUEST,true)."\n",\FILE_APPEND);

if($action=='query'){
    
    success([
        "pid"=>$_REQUEST['pid'],
        "key"=>$_REQUEST['key'],
        "qq"=>"0",
        "paymb"=>"99999999",
        "outtime"=>"120",
        "repeat"=>"1",
        "money_nums"=>"5",
        "login"=>0,
        "addtime"=>date('Y-m-d H:i:s'),
        "active"=>"1"
    ]);
}else if($action=='submit'){
    $apikey = $_REQUEST['apikey'];
    $pid = $_REQUEST['pid'];
    $key = $_REQUEST['key'];
    $pay_id = $_REQUEST['pay_id'];
    $outtime = $_REQUEST['outtime'];
    $trade_no = $_REQUEST['trade_no'];
    $apikey = $_REQUEST['apikey'];
    $out_trade_no = $_REQUEST['out_trade_no'];
    $notify_url = $_REQUEST['notify_url'];
    $type = $_REQUEST['type'];
    $name = $_REQUEST['name'];
    $money = $_REQUEST['money'];
    $sign = $_REQUEST['sign'];
    // file_put_contents('./test.txt',var_export($_REQUEST,true),\FILE_APPEND);
    //判断订单是否存在
    $rows=$DB->getRow("select * from ucenter_order where type='$type' and pid='$pid' and trade_no='$trade_no' or out_trade_no='$out_trade_no' limit 1");
    if($rows){
           file_put_contents(__DIR__.'/err1.txt', "订单号已存在，请重试！\n",\FILE_APPEND);
    	error('订单号已存在，请重试！');
    }
    $time  = time();
    $rows=$DB->getRow("select * from ucenter_order where  name='$name' and type='$type' and pid='$pid' and  status=0 and pay_money='$money' and outtime>'$time' limit 1");
    $pay_money  = $money;
    if($rows){
        $maxMoney = $money+0.1;
        $moneys=$DB->getAll("select * from ucenter_order where name='$name' and  type='$type' and pid='$pid' and  status=0 and pay_money>'$money' and pay_money <='$maxMoney' and outtime>'$time' order by pay_money");
        // file_put_contents(__DIR__.'/err1.txt',var."\n",\FILE_APPEND);
        if($moneys){
             $moneys = array_column($moneys,'pay_money',null);
            for($i=0.01;$i<0.1;$i+=0.01){
                if(!in_array(($money+$i).'',$moneys)){
                    //可以使用的价格
                    $pay_money = $money+$i;
                    break;
                }
            }
        }else{
              $pay_money = $money+0.01;
        }
       
        if($pay_money==$money){
         file_put_contents(__DIR__.'/err1.txt', "业务繁忙，请重新提交订单！\n",\FILE_APPEND);

            error('业务繁忙，请重新提交订单！');
        }
    
    // echo '订单价格相同';
    // exit();
    }
    // $appkey = md5(mt_rand(0,999).time());
    $sql="insert into `ucenter_order` (`pid`,`outtime`,`trade_no`,`out_trade_no`,`notify_url`,`type`,`name`,`money`,`pay_money`,`apitime`,`addtime`,`sign`) values (:pid, :outtime, :trade_no, :out_trade_no, :notify_url, :type, :name, :money,:pay_money,:apitime,:addtime,:sign)";
    $res =$DB->exec($sql, [
        'pid'=>$pid, 
        'outtime'=>time()+$outtime, 
        'trade_no'=>$trade_no, 
        'out_trade_no'=>$out_trade_no,
        'notify_url'=>$notify_url, 
        'type'=>$type,
        'name'=>$name,
        'money'=>$money,
        'pay_money'=>$pay_money,
        'apitime'=>time(),
        'sign'=>$sign,
        'addtime'=>time(),
    ]);
    if($res!==false){
        success([
            "money"=>$pay_money,
            'pid' => $pid,
            'login' => 0,
            'out_trade_no' =>$out_trade_no,
            'name' => $name,
            'date' =>date('Y-m-d H:i:s')
        ],'记录订单成功');
    }else{
    	error($DB->error());
    }
}
}catch(\Exception $e){
    file_put_contents(__DIR__.'/err.txt', $e->getMessage()."\n",\FILE_APPEND);
}